import { request } from '../../utils/request'
import type { Role, ApiResponse } from '../../types/app.d'

/**
 * 获取角色列表
 */
export const getRoleList = (params: {
  page: number
  pageSize: number
  name?: string
  code?: string
  status?: number
  beginTime?: string
  endTime?: string
}) => {
  return request.get<ApiResponse<{
    list: Role[]
    total: number
  }>>('/system/role/list', { params })
}

/**
 * 获取角色详情
 */
export const getRoleInfo = (id: string) => {
  return request.get<ApiResponse<Role>>(`/system/role/${id}`)
}

/**
 * 创建角色
 */
export const createRole = (data: Omit<Role, 'id' | 'createTime' | 'updateTime'>) => {
  return request.post<ApiResponse>('/system/role', data)
}

/**
 * 更新角色
 */
export const updateRole = (id: string, data: Partial<Omit<Role, 'id' | 'createTime'>>) => {
  return request.put<ApiResponse>(`/system/role/${id}`, data)
}

/**
 * 删除角色
 */
export const deleteRole = (id: string) => {
  return request.delete<ApiResponse>(`/system/role/${id}`)
}

/**
 * 批量删除角色
 */
export const batchDeleteRole = (ids: string[]) => {
  return request.delete<ApiResponse>('/system/role/batch', { data: { ids } })
}

/**
 * 修改角色状态
 */
export const changeRoleStatus = (id: string, status: number) => {
  return request.put<ApiResponse>(`/system/role/${id}/status`, { status })
}

/**
 * 获取角色菜单权限
 */
export const getRoleMenus = (id: string) => {
  return request.get<ApiResponse<string[]>>(`/system/role/${id}/menus`)
}

/**
 * 保存角色菜单权限
 */
export const saveRoleMenus = (id: string, menuIds: string[]) => {
  return request.put<ApiResponse>(`/system/role/${id}/menus`, { menuIds })
}